<?php


namespace app\admin\model;

use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use support\util\TreeArray;

/**
 * 角色
 */
class Role extends \support\Model
{

    protected $fillable = [
        'pid',
        'nickname',
        'status'
    ];

    /**
     * @return BelongsToMany
     */
    public function menus()
    {
        return $this->belongsToMany(Menu::class, 'role_menu_access', 'role_id', 'menu_id')->whereNotIn('status', ['disibled']);
    }

    public function admins()
    {
        return $this->belongsToMany(Admin::class, 'admin_role_access', 'admin_id', 'role_id');
    }

    public static function getStatusList()
    {
        return ['normal' => '正常', 'disibled' => '禁用'];
    }

    /**
     * 获取树形选择
     * @return array
     */
    public static function getTreeSelect($id = 0)
    {
        $tree = self::getTree();
        $list = $tree->getTreeArray($id);
        return $tree->getTreeList($list, 'nickname');
    }

    public static function getTreeArray($id = 0)
    {
        $tree = self::getTree();
        return $tree->getTreeArray($id);
    }

    /**
     * 获取树对象 查询所有角色
     * @return TreeArray
     */
    public static function getTree()
    {
        $list = self::query()->get();
        $tree = TreeArray::instance();
        $tree->init($list->toArray());
        return $tree;
    }

}
